﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using DTO;

namespace DAO
{
    public class PhimDAO
    {
        public DataTable LoadPhim()
        {
            DataTable dt = new DataTable();
            SqlConnection cn = DataProvider.ConnectionData();
            SqlCommand cmd = new SqlCommand("LayPhim", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);
            cn.Close();
            return dt;
        }
        public void ThemPhim(PhimDTO phim)
        {
            SqlConnection cn = DAO.DataProvider.ConnectionData();
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "ThemPhim";
            cmd.Connection = cn;
            // Tao tham so cho SqlCommand
            SqlParameter para;
            para = new SqlParameter("maphim", SqlDbType.NVarChar, 10);
            para.Direction = ParameterDirection.Input;
            para.Value = phim.Maphim;
            cmd.Parameters.Add(para);
            para = new SqlParameter("tenphim", SqlDbType.NVarChar, 100);
            para.Direction = ParameterDirection.Input;
            para.Value = phim.Tenphim;
            cmd.Parameters.Add(para);
            para = new SqlParameter("namsx", SqlDbType.Int);
            para.Direction = ParameterDirection.Input;
            para.Value = phim.Namsx;
            cmd.Parameters.Add(para);
            para = new SqlParameter("Nuocsx", SqlDbType.NVarChar, 20);
            para.Direction = ParameterDirection.Input;
            para.Value = phim.Nuocsx;
            cmd.Parameters.Add(para);
            para = new SqlParameter("hangphim", SqlDbType.NVarChar, 20);
            para.Direction = ParameterDirection.Input;
            para.Value = phim.Hangphim;
            cmd.Parameters.Add(para);
            para = new SqlParameter("gioithieu", SqlDbType.NVarChar, 300);
            para.Direction = ParameterDirection.Input;
            para.Value = phim.Gioithieu;
            cmd.Parameters.Add(para);
            para = new SqlParameter("trailer", SqlDbType.NVarChar, 200);
            para.Direction = ParameterDirection.Input;
            para.Value = phim.Trailer;
            cmd.Parameters.Add(para);
            para = new SqlParameter("poster", SqlDbType.NVarChar, 200);
            para.Direction = ParameterDirection.Input;
            para.Value = phim.Poster;
            cmd.Parameters.Add(para);
            para = new SqlParameter("time", SqlDbType.Int);
            para.Direction = ParameterDirection.Input;
            para.Value = phim.Time;
            cmd.Parameters.Add(para);
            para = new SqlParameter("Dinhdang", SqlDbType.NVarChar, 5);
            para.Direction = ParameterDirection.Input;
            para.Value = phim.Dinhdang;
            cmd.Parameters.Add(para);
            para = new SqlParameter("maloai", SqlDbType.NVarChar, 10);
            para.Direction = ParameterDirection.Input;
            para.Value = phim.Maloai;
            cmd.Parameters.Add(para);
            para = new SqlParameter("dsdv", SqlDbType.NVarChar, 300);
            para.Direction = ParameterDirection.Input;
            para.Value = phim.Dsdv;
            cmd.Parameters.Add(para);
            para = new SqlParameter("matinhtrang", SqlDbType.Int);
            para.Value = phim.Tinhtrang;
            cmd.Parameters.Add(para);
            // Thuc thi viec goi Storeproc
            cmd.ExecuteNonQuery();
            cn.Close();
        }
        public void CapNhatThongTinphim(PhimDTO phim)
        {
            // Tao doi tuong SqlCommand
            SqlConnection cn = DataProvider.ConnectionData();
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "SuaPhim";
            cmd.Connection = cn;
            // Tao tham so cho SqlCommand
            SqlParameter para;
            para = new SqlParameter("maphim", SqlDbType.NVarChar, 10);
            para.Direction = ParameterDirection.Input;
            para.Value = phim.Maphim;
            cmd.Parameters.Add(para);
            para = new SqlParameter("tenphim", SqlDbType.NVarChar, 100);
            para.Direction = ParameterDirection.Input;
            para.Value = phim.Tenphim;
            cmd.Parameters.Add(para);
            para = new SqlParameter("namsx", SqlDbType.Int);
            para.Direction = ParameterDirection.Input;
            para.Value = phim.Namsx;
            cmd.Parameters.Add(para);
            para = new SqlParameter("Nuocsx", SqlDbType.NVarChar, 20);
            para.Direction = ParameterDirection.Input;
            para.Value = phim.Nuocsx;
            cmd.Parameters.Add(para);
            para = new SqlParameter("hangphim", SqlDbType.NVarChar, 20);
            para.Direction = ParameterDirection.Input;
            para.Value = phim.Hangphim;
            cmd.Parameters.Add(para);
            para = new SqlParameter("gioithieu", SqlDbType.NVarChar, 300);
            para.Direction = ParameterDirection.Input;
            para.Value = phim.Gioithieu;
            cmd.Parameters.Add(para);
            para = new SqlParameter("trailer", SqlDbType.NVarChar, 200);
            para.Direction = ParameterDirection.Input;
            para.Value = phim.Trailer;
            cmd.Parameters.Add(para);
            para = new SqlParameter("poster", SqlDbType.NVarChar, 200);
            para.Direction = ParameterDirection.Input;
            para.Value = phim.Poster;
            cmd.Parameters.Add(para);
            para = new SqlParameter("time", SqlDbType.Int);
            para.Direction = ParameterDirection.Input;
            para.Value = phim.Time;
            cmd.Parameters.Add(para);
            para = new SqlParameter("Dinhdang", SqlDbType.NVarChar, 5);
            para.Direction = ParameterDirection.Input;
            para.Value = phim.Dinhdang;
            cmd.Parameters.Add(para);
            para = new SqlParameter("maloai", SqlDbType.NVarChar, 10);
            para.Direction = ParameterDirection.Input;
            para.Value = phim.Maloai;
            cmd.Parameters.Add(para);
            para = new SqlParameter("dsdv", SqlDbType.NVarChar, 300);
            para.Direction = ParameterDirection.Input;
            para.Value = phim.Dsdv;
            cmd.Parameters.Add(para);
            para = new SqlParameter("matinhtrang", SqlDbType.Int);
            para.Direction = ParameterDirection.Input;
            para.Value = phim.Tinhtrang;
            cmd.Parameters.Add(para);
            // Thuc thi viec goi Storeproc
            cmd.ExecuteNonQuery();
            cn.Close();
        }
        public void XoaPhim(string maphim)
        {
            // Tao doi tuong SqlCommand
            SqlConnection cn = DataProvider.ConnectionData();
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "XoaPhim";
            cmd.Connection = cn;
            // Tao tham so cho SqlCommand
            SqlParameter para;
            para = new SqlParameter("maphim", SqlDbType.NVarChar, 10);
            para.Direction = ParameterDirection.Input;
            para.Value = maphim;
            cmd.Parameters.Add(para);
            // Thuc thi viec goi Storeproc
            cmd.ExecuteNonQuery();
            cn.Close();
        }
        public DataTable LayMaPhim(string maphim)
        {
            DataTable dt = new DataTable();
            SqlConnection cn = DataProvider.ConnectionData();
            SqlCommand cmd = new SqlCommand("LayMaPhim", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@maphim", SqlDbType.NVarChar, 10);
            cmd.Parameters["@maphim"].Value = maphim;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);
            cn.Close();
            return dt;
        }
    }
}
